//action.js
//获取应用实例
var app = getApp()
var httpUtil = require("../../utils/http.js");
Page({
  data: {
    hidden: false,
    lastTapTime: 0,
    list: [],
    current:1,
    size:10,
    scrollTop: 0,
    scrollHeight: 0,
    triggered:false,
    // 触发显示按钮的滑动距离阈值
    isLastPage: false, // 是否是最后一页
    no_scroll: false,
  },
  
  onLoad: function () {
    //注意 scroll-view 必须要设置高度才能监听滚动事件，需要在页面的onLoad事件中给scroll-view的高度赋值
    var that = this;
    wx.getSystemInfoAsync({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight,
          list: [],
          current:1,
          size:10,
          isLastPage: false, // 是否是最后一页
        });
        that.loadMore()
      }
    });
  },

  //上拉加载
  bindDownLoad: function () {
    console.log("bindDownLoad===1111111111111111")
    this.loadMore();
  },
  //下拉刷新
  bindRefresh: function () {
    console.log("bindRefresh===1111111111111111")
    //数据刷新
    this.setData({
      current: 1,
      list: [],
      triggered:true,
    });
    this.loadFirst();
  },
  onTabItemTap(item) {
    // 获取当前时间戳
    const currentTime = new Date().getTime();
    if (currentTime - this.data.lastTapTime < 300) {
       // 双击后的逻辑处理
       this.bindRefresh();
    }
    this.setData({
       lastTapTime: currentTime
    })
   // 处理底部 tab 被点击的逻辑
 },
  //加载更多
  loadMore:function () {
    if(this.data.isLastPage){
       return;
    }
    let that = this;
    const params = {
      url:  '/api/open/interface/getResult' ,
      method: "POST",
      data: {
        interfaceCode: "SI0021",
        "page":that.data.current,
        "size":that.data.size
      },
      callBack: (res) => {
        let list = that.data.list ;
        let records = res.data.rows;
        for(var i = 0; i < records.length; i++){
          list.push(records[i]);
        }
        if( records.length==that.data.size){
          that.setData({
            list: list,
            current:that.data.current+1,
            isLastPage: false
          });
        }else{
          that.setData({
            list: list,
            isLastPage: true
          });
        }
      },
      errCallBack:(res) =>{
        
      }
    };
    httpUtil.request(params,false);
  },
  //首次加载
  loadFirst:function () {
    let that = this;
    const params = {
      url:  '/api/open/interface/getResult' ,
      method: "POST",
      data: {
        interfaceCode: "SI0021",
        "page":that.data.current,
        "size":that.data.size
      },
      callBack: (res) => {
        let list = that.data.list ;
        let records = res.data.rows;
        for(var i = 0; i < records.length; i++){
          list.push(records[i]);
        }
        if( records.length==that.data.size){
          that.setData({
            list: list,
            current:that.data.current+1,
            isLastPage: false,
            triggered:false
          });
        }else{
          that.setData({
            list: list,
            isLastPage: true,
            triggered:false
          });
        }
      }
    };
    httpUtil.request(params,false);
  },
  gotoMediaDetail: function(event){
    let mediaUrl= event.currentTarget.dataset['url'];
    console.log("mediaUrl=",mediaUrl)
    wx.navigateTo({
      url: '/pages/mediaDetail/mediaDetail?mediaUrl=' + mediaUrl,
    })
  },
  goTop: function(){
    console.log(111111)
    this.setData({
      scrollTop:0
    })
  }
})